layui.use(['form', 'table','layuimini','element', 'laydate', 'tableSelect'], function () {
    var $ = layui.jquery,
        form = layui.form,
        table = layui.table,
        layuimini = layui.layuimini,
        laydate = layui.laydate,
        tableSelect = layui.tableSelect;

    /**
     * 初始化表单，要加上，不然刷新部分组件可能会不加载
     */
    form.render();

    var BoInfoSendMrg = {
        id: 'BoInfoSendMrgTable'
    };

    var column = [
        [
            {type: 'numbers'},
            {field: 'msgTemName', title: '短信模板', width: 140, align: 'center',
                templet: function (object) {
                    if(object.msgTemName != "" && object.msgTemName != null){
                        return object.msgTemName;
                    }else{
                        return "-";
                    }
                }
            },
            {field: 'markTitle', title: '纪念日', width: 120, align: 'center',
                templet: function (object) {
                    if(object.markTitle != "" && object.markTitle != null){
                        return object.markTitle;
                    }else{
                        return "-";
                    }
                }
            },
            {field: 'msgType', title: '发送类型', width: 100, align: 'center',
                templet: function (object) {
                    if(object.msgType == 0){
                        return '<span class="layui-badge layui-bg-green">生日提醒</span>';
                    }else if(object.msgType == 1){
                        return '<span class="layui-badge layui-bg-orange">纪念节日</span>';
                    } else if(object.msgType == 2){
                        return '<span class="layui-badge layui-bg-blue">普通节日</span>';
                    } else if(object.msgType == 3){
                        return '<span class="layui-badge layui-bg-cyan">备忘事项</span>';
                    } else if(object.msgType == 4){
                        return '<span class="layui-badge layui-bg-red">看书动态</span>';
                    }else if(object.msgType == 5){
                        //layui-bg-gray
                        return '<span class="layui-badge" style="background-color: #fd55dd;">恋爱清单</span>';
                    }else{
                        return "-";
                    }
                }
            },
            {field: 'sendStatus', title: '请求状态', width: 100, align: 'center',
                templet: function (object) {
                    if(object.sendStatus == 0){
                        return '<span class="layui-badge layui-bg-green">请求成功</span>';
                    }else if(object.sendStatus == 1){
                        return '<span class="layui-badge layui-bg-red">请求失败</span>';
                    }else{
                        return "-";
                    }
                }
            },
            {field: 'receiveName', title: '接收人', width: 100, align: 'center'},
            {field: 'receivePhone', title: '接收手机', width: 130, align: 'center'},
            {field: 'sendDate', title: '发送时间', width: 180, align: 'center'},
            {field: 'sendResult', title: '发送状态', width: 100, align: 'center',
                templet: function (object) {
                    if(object.sendResult == 1){
                        return '<span class="layui-badge layui-bg-blue">等待回执</span>';
                    }else if(object.sendResult == 2){
                        return '<span class="layui-badge layui-bg-red">发送失败</span>';
                    } else if(object.sendResult == 3){
                        return '<span class="layui-badge layui-bg-green">发送成功</span>';
                    }else{
                        return "-";
                    }
                }
            },
            {field: 'againSend', title: '再次发送', width: 90, align: 'center',
                templet: function (object) {
                    if(object.againSend == 0){
                        return '<span class="layui-badge layui-bg-blue">否</span>';
                    }else if(object.againSend == 1){
                        return '<span class="layui-badge layui-bg-red">是</span>';
                    }else{
                        return "-";
                    }
                }
            },
            {field: 'bizId', title: '回执ID', align: 'center'},
            {field: 'errorContent', title: '错误内容', align: 'center'},
            {field: 'remark', title: '发送结果信息', align: 'center'},
            {field: 'content', title: '发送内容', align: 'center'},
            {templet: '#stateBtn', width: 80, align: 'center', title: '操作'}
        ]
    ];

    table.render({
        elem: '#' + BoInfoSendMrg.id,
        url: '/web/boInfoSendMrg/list',
        toolbar: '#toolbarBtn',
        defaultToolbar: ['filter', 'exports', 'print'],
        cols: column,
        limits: [10, 15, 20, 25, 50, 100],
        limit: 15,
        page: true
    });

    // 监听搜索操作
    $("#data-search-btn").on('click', function () {

        //获取搜索条件
        var msgTemId = $("#search_msgTemId").val();
        var markId = $("#search_markId").val();
        var msgType = $("#search_msgType").val();
        var sendStatus = $("#search_sendStatus").val();
        var sendResult = $("#search_sendResult").val();
        var sendDate = $("#search_sendDate").val();
        var receiveName = $("#search_receiveName").val();
        var receivePhone = $("#search_receivePhone").val();

        //封装
        var dataObject = {};
        dataObject['msgTemId'] = msgTemId;
        dataObject['markId'] = markId;
        dataObject['msgType'] = msgType;
        dataObject['sendStatus'] = sendStatus;
        dataObject['sendResult'] = sendResult;
        dataObject['sendDate'] = sendDate;
        dataObject['receiveName'] = receiveName;
        dataObject['receivePhone'] = receivePhone;
        var result = JSON.stringify(dataObject);

        //执行搜索重载
        table.reload(BoInfoSendMrg.id, {
            page: {
                curr: 1
            }
            , where: {
                searchParams: result
            }
        }, 'data');

        return false;
    });

    // 监听重置操作
    $("#data-reset-btn").on('click', function () {

        //初始化
        $("#search_msgTemId").val("");
        $("#search_msgTemName").val("");
        $("#search_markId").val("");
        $("#search_markTitle").val("");
        $("#search_msgType").val("-1");
        $("#search_sendStatus").val("-1");
        $("#search_sendResult").val("-1");
        $("#search_sendDate").val("");
        $("#search_receiveName").val("");
        $("#search_receivePhone").val("");

        form.render();

        //封装
        var dataObject = {};
        dataObject['sendDate'] = "";
        dataObject['msgTemId'] = "";
        dataObject['markId'] = "";
        dataObject['msgType'] = "-1";
        dataObject['sendStatus'] = "-1";
        dataObject['sendResult'] = "-1";
        dataObject['receiveName'] = "";
        dataObject['receivePhone'] = "";
        var result = JSON.stringify(dataObject);

        //执行搜索重载
        table.reload(BoInfoSendMrg.id, {
            page: {
                curr: 1
            }
            , where: {
                searchParams: result
            }
        }, 'data');

        return false;
    });

    //下拉选择
    //默认值:只需要在触发input上添加 ts-selected="1,2,3" 属性即可 值需与checkedKey对应
    tableSelect.render({
        elem: '#search_msgTemName',	//定义输入框input对象 必填
        checkedKey: 'id', //表格的唯一建值，非常重要，影响到选中状态 必填
        searchKey: 'name',	//搜索输入框的name值 默认keyword
        searchPlaceholder: '短信模板搜索',	//搜索输入框的提示文字 默认关键词搜索
        table: {	//定义表格参数，与LAYUI的TABLE模块一致，只是无需再定义表格elem
            url:'/web/boMsgTemplate/list',
            cols: [
                [
                    {type: 'radio'},
                    {field: 'msgSignName', title: '短信签名', align: 'center', width: 140},
                    {field: 'name', title: '模板名称', align: 'center', width: 140},
                    {field: 'code', title: '模板编号', align: 'center', width: 150},
                    {field: 'remark', title: '备注', align: 'center', width: 150}
                ]
            ],
            limits: [10, 15, 20, 25, 50, 100],
            limit: 15,
            page: true
        },
        done: function (elem, data) {
            //选择完后的回调，包含2个返回值 elem:返回之前input对象；data:表格返回的选中的数据 []
            //拿到data[]后 就按照业务需求做想做的事情啦~比如加个隐藏域放ID...
            $("#search_msgTemName").val(data.data[0].name);
            $("#search_msgTemId").val(data.data[0].id);
        }
    });

    //下拉选择
    //默认值:只需要在触发input上添加 ts-selected="1,2,3" 属性即可 值需与checkedKey对应
    tableSelect.render({
        elem: '#search_markTitle',	//定义输入框input对象 必填
        checkedKey: 'id', //表格的唯一建值，非常重要，影响到选中状态 必填
        searchKey: 'title',	//搜索输入框的name值 默认keyword
        searchPlaceholder: '纪念日搜索',	//搜索输入框的提示文字 默认关键词搜索
        table: {	//定义表格参数，与LAYUI的TABLE模块一致，只是无需再定义表格elem
            url:'/web/boMarkDate/listAll',
            cols: [
                [
                    {type: 'radio'},
                    {field: 'title', title: '标题', align: 'center', width: 120},
                    {field: 'remarkTypeName', title: '提醒类型', align: 'center', width: 120},
                    {field: 'markDateValue', title: '日期', align: 'center', width: 120},
                    {field: 'dateTypeName', title: '日期类型', align: 'center', width: 100},
                    {field: 'runTimeTypeName', title: '提醒时间类型', align: 'center', width: 130},
                    {field: 'isOpenName', title: '是否开启提醒', align: 'center', width: 130},
                    {field: 'delFlagName', title: '是否已删除', align: 'center', width: 120}
                ]
            ],
            limits: [10, 15, 20, 25, 50, 100],
            limit: 15,
            page: true
        },
        done: function (elem, data) {
            //选择完后的回调，包含2个返回值 elem:返回之前input对象；data:表格返回的选中的数据 []
            //拿到data[]后 就按照业务需求做想做的事情啦~比如加个隐藏域放ID...
            $("#search_markTitle").val(data.data[0].title);
            $("#search_markId").val(data.data[0].id);
        }
    });

    //时间
    laydate.render({
        elem: '#search_sendDate'
    });

    /**
     * 点击详情按钮
     * @param id
     */
    BoInfoSendMrg.edit = function(id){
        var content = layuimini.getHrefContent('/web/boInfoSendMrg/addOrEdit?id=' + id);
        var index = layer.open({
            title: '信息详情',
            type: 1,
            shade: 0.2,
            maxmin:true,
            shadeClose: false,
            zIndex: 998,
            area: ['600px','500px'], //宽高
            content: content
        });

        return false;
    };


    //监听表格复选框选择
    // table.on('checkbox(currentTableFilter)', function (obj) {
    //     console.log(obj)
    // });

    //监听工具条
    table.on('tool(' + BoInfoSendMrg.id + ')', function (obj) {
        var data = obj.data;
        var layEvent = obj.event;

        if (layEvent === 'edit') {
            BoInfoSendMrg.edit(data.id)
        }
    });

});